//
//  YWCommonTool.m
//  maintain
//
//  Created by 郑想 on 15/10/20.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "YWCommonTool.h"
#import "GTMBase64.h"
#import "MBProgressHUD.h"
#import "NSDate+Additions.h"
#import "YYKit.h"
#import "UIColor+CustomColors.h"
#import "WMHttpTool.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "YWDeviceInfo.h"
#import "sys/utsname.h"
#import <CommonCrypto/CommonDigest.h>

#import "YWLoginTool.h"
#import "YWHandler.h"
#import "YWKeepWatchMission.h"
#import "YWDictMaintenanceType.h"
#import "YWInspectionMissionModel.h"

#import "YWKeepWatchMissionManager.h"
#import "YWInspectionMissionManager.h"

NSString* const KTimeFormatDateAndTime = @"%Y-%m-%d %H:%M";//yyyy-MM-dd HH:mm
NSString* const KTimeFormatDate = @"%Y-%m-%d";//yyyy-MM-dd
NSString* const KTimeFormatYearMonth = @"%Y-%m";//yyyy-MM
NSString* const KTimeFormatChineseDate = @"%Y年%m月%d日";//@"yyyy年MM月dd日
NSString* const KTimeFormatFullTime = @"%Y年%m月%d日 %H:%M";//yyyy年MM月dd日 HH:mm

NSString* const KTimeFormatShortTime = @"%H:%M";//HH:mm
NSString* const KTimeFormatShortDate = @"%m/%d";//MM/dd
NSString* const KTimeFormatShortTimeDate = @"%H:%M %m/%d";//HH:mm MM/dd
NSString* const KTimeFormatShortDateTime = @"%m-%d %H:%M";//MM-dd HH:mm
NSString* const KTimeFormatShortTimeDateYear = @"%H:%M %Y/%m/%d";//HH:mm yyyy/MM/dd
NSString* const KTimeFormatShortYearDateTime = @"%Y/%m/%d %H:%M";//yyyy/MM/dd HH:mm
NSString* const KTimeFormatWithOutDayMonth = @"%d %m月";//dd MM月
NSString* const KTimeFormatDayMonth = @"%d日%m月";//dd日MM月
NSString* const KTimeFormatMonthDay = @"%m月%d日";//MM月dd日
NSString* const KTimeFormatMonthDayAndTime = @"%m月%d日 %H:%M";//MM月dd日 HH:mm
NSString* const KTimeFormatFullDate = @"%Y/%m/%d";//yyyy/MM/dd
NSString *const KTimeFormatFullDateAndTime = @"%Y-%m-%d %H:%M:%S";//yyyy-MM-dd HH:mm:ss

@implementation YWCommonTool

#pragma mark - 登陆相关url(api/base)

+ (NSString*)preLoginUrl{
    return [NSString stringWithFormat:@"%@/api/base/account/preLogin", kHttpBaseUrl];
}

+ (NSString*)loginUrl{
    return [NSString stringWithFormat:@"%@/api/base/account", kHttpBaseUrl];
}

+ (NSString*)logoutUrl{
    return [NSString stringWithFormat:@"%@/api/base/account/logout", kHttpBaseUrl];
}

+ (NSString*)modifyPasswordUrl{
    return [NSString stringWithFormat:@"%@/api/base/account/password", kHttpBaseUrl];
}

+ (NSString*)modifySelfInfoUrl{
    return [NSString stringWithFormat:@"%@/api/base/user", kHttpBaseUrl];
}

+ (NSString*)getBuildingJsonUrl{
    return [NSString stringWithFormat:@"%@/api/base/building/json", kHttpBaseUrl];
}

+ (NSString*)getBuildingMembersUrl{
    return [NSString stringWithFormat:@"%@/api/base/contact/building", kHttpBaseUrl];
}

+ (NSString*)getOrganizationMembersUrl{
    return [NSString stringWithFormat:@"%@/api/base/contact/organization", kHttpBaseUrl];
}

+ (NSString*)ewinMenuUrl{
    return [NSString stringWithFormat:@"%@/api/base/permission", kHttpBaseUrl];
}

+ (NSString*)loginUserInfoUrl{
    return [NSString stringWithFormat:@"%@/api/base/permission/identity", kHttpBaseUrl];
}

+ (NSString*)noQrcodeRightsUrl{
    return [NSString stringWithFormat:@"%@/api/mix/module/config", kHttpBaseUrl];
}

#pragma mark - 图片地址

+ (NSString*)pictureDownloadUrl:(NSString*)shortUrl
{
    return [NSString stringWithFormat:@"%@%@", kAliyunImageUrl, shortUrl];
}

#pragma mark - 反馈

+ (NSString*)getFeedBackUrl
{
    return [NSString stringWithFormat:@"%@/api/mix/feedback", kHttpBaseUrl];
}

#pragma mark - 上传设备信息

+ (NSString*)getUploadDeviceInfoUrl
{
    return [NSString stringWithFormat:@"%@/api/mix/device", kHttpBaseUrl];
}

#pragma mark - 所有模块未读数

+ (NSString *)unReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/count", kHttpBaseUrl];
}

+ (NSString*)noticeUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/notice", kHttpBaseUrl];
}

+ (NSString*)tempMissionUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/task", kHttpBaseUrl];
}

+ (NSString*)troubleUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/trouble", kHttpBaseUrl];
}

+ (NSString*)fixTroubleUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/fixTrouble", kHttpBaseUrl];
}

+ (NSString*)inspectionUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/inspection", kHttpBaseUrl];
}

+ (NSString*)keepWatchUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/keepwatch", kHttpBaseUrl];
}

+ (NSString*)detectionUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/detection", kHttpBaseUrl];
}

+ (NSString*)upkeepUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/upkeep", kHttpBaseUrl];
}

+ (NSString*)materialUnReadCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/index/unread/material", kHttpBaseUrl];
}

#pragma mark - 设备相关

+ (NSString*)getEquipmentTypeUrl{
    return [NSString stringWithFormat:@"%@/api/equipment/type", kHttpBaseUrl];
}

+ (NSString*)getEquipmentRuntimeParameterUrl{
    return [NSString stringWithFormat:@"%@/api/equipment/field", kHttpBaseUrl];
}

+ (NSString*)getSingleEquipmentRuleUrl{
    return [NSString stringWithFormat:@"%@/api/equipment/rule", kHttpBaseUrl];
}

+ (NSString*)getEquipmentUseMethodUrl{
    return [NSString stringWithFormat:@"%@/api/equipment/applicationMethod", kHttpBaseUrl];
}

+ (NSString*)getEquipmentUseRangeUrl{
    return [NSString stringWithFormat:@"%@/api/equipment/applicationRange", kHttpBaseUrl];
}

+ (NSString*)getQrcodeDataUrl{
    return [NSString stringWithFormat:@"%@/api/equipment/qrcode", kHttpBaseUrl];
}

////监控报警
+ (NSString*)getWarnUrl{
    return [NSString stringWithFormat:@"%@/api/equipment/warn", kHttpBaseUrl];
}

#pragma mark - 检测保养(detectAndUpkeep)

+ (NSString*)getAllMissionUrl{
    return [NSString stringWithFormat:@"%@/api/du/mission", kHttpBaseUrl];
}

+ (NSString*)getDetectionMissionUrl{
    return [NSString stringWithFormat:@"%@/api/du/mission/detection", kHttpBaseUrl];
}

+ (NSString*)getUpkeepMissionUrl{
    return [NSString stringWithFormat:@"%@/api/du/mission/upkeep", kHttpBaseUrl];
}

+ (NSString*)getSubmitMissionUrl{
    return [NSString stringWithFormat:@"%@/api/du/record", kHttpBaseUrl];
}

+ (NSString*)getMissionRecordUrl{
    return [NSString stringWithFormat:@"%@/api/du/record/mission", kHttpBaseUrl];
}

+ (NSString*)getEquipmentDetectAndUpkeepRecordUrl{
    return [NSString stringWithFormat:@"%@/api/du/record/equipment", kHttpBaseUrl];
}

+ (NSString*)getMissionStatusUrl{
    return [NSString stringWithFormat:@"%@/api/du/mission/status",kHttpBaseUrl];
}

+ (NSString*)getAssignMissionCountUrl{
    return [NSString stringWithFormat:@"%@/api/du/mission/dict",kHttpBaseUrl];
}

#pragma mark - 维修(trouble)

+ (NSString*)getDesignTroubleUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/mission", kHttpBaseUrl];
}

+ (NSString*)getTroubleMissionUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/mission", kHttpBaseUrl];
}

+ (NSString*)getReportTroubleBillsOrRecords{
    return [NSString stringWithFormat:@"%@/api/trouble/report",kHttpBaseUrl];
}

+ (NSString*)getReportTroubleAssignOrNotUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/report/distribution", kHttpBaseUrl];
}

+ (NSString*)getMyReportTroubleUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/report/personal", kHttpBaseUrl];
}

+ (NSString*)getEquipmentTroubleRecordUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/report",  kHttpBaseUrl];
}

+ (NSString*)getLocationTroubleRecordUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/report",  kHttpBaseUrl];
}

+ (NSString*)getSubmitTroubleMissionUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/record", kHttpBaseUrl];
}

+ (NSString*)getSubmitPostTroubleUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/report", kHttpBaseUrl];
}

+ (NSString*)postSubscriptionReportTroubleUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/userTroubleSubscription", kHttpBaseUrl];
}

+ (NSString*)requestSubscriptionReportTroubleUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/userTroubleSubscription/personal", kHttpBaseUrl];
}

+ (NSString*)getTroubleRecordUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/record", kHttpBaseUrl];
}

+ (NSString*)getTroubleSituationUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/report/status",kHttpBaseUrl];
}

+ (NSString*)getCirculationInfoUrl
{
    return [NSString stringWithFormat:@"%@/api/trouble/report", kHttpBaseUrl];
}

+ (NSString*)cancelPostTroubleUrl
{
    return [NSString stringWithFormat:@"%@/api/trouble/report/cancel", kHttpBaseUrl];
}

+ (NSString*)cancelTroubleMissionUrl
{
    return [NSString stringWithFormat:@"%@/api/trouble/mission/cancel", kHttpBaseUrl];
}

+ (NSString*)rejectTroubleMissionUrl
{
    return [NSString stringWithFormat:@"%@/api/trouble/mission/reject", kHttpBaseUrl];
}


#pragma mark - 巡更

+ (NSString*)getKeepWatchMissionUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/mission", kHttpBaseUrl];
}

+ (NSString*)getKeepWatchMissionInfoUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/record/latest", kHttpBaseUrl];
}

+ (NSString*)getKeepWatchMissionStatusUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/mission/status", kHttpBaseUrl];
}

+ (NSString*)getPatrolLineRecordUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/record", kHttpBaseUrl];
}

+ (NSString*)getPatrolLineDetailUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/patrolLine/details", kHttpBaseUrl];
}

+ (NSString *)getTheLatestPointRecordUrl{
    return [NSString stringWithFormat:@"%@/api/keepWatch/record/latest", kHttpBaseUrl];
}

+ (NSString*)getKeepWatchLocationRecordUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/record/location", kHttpBaseUrl];
}

+ (NSString*)getSubmitKeepWatchRecordUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/record", kHttpBaseUrl];
}

+ (NSString*)getAllPatrolLineUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/patrolLine/option", kHttpBaseUrl];
}

+ (NSString*)getOnePatrolLineUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/patrolLine", kHttpBaseUrl];
}

#pragma mark - 签到(考勤)

+ (NSString*)getAttendanceUrl{
    return [NSString stringWithFormat:@"%@/api/mix/attendance", kHttpBaseUrl];
}

+ (NSString*)getAttendanceRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/attendance", kHttpBaseUrl];
}

+ (NSString*)getAboutMeAttendanceRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/attendance/personalRelation", kHttpBaseUrl];
}

#pragma mark - 抄表

+ (NSString*)getNewestReadingMemterRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/meter/record/latest", kHttpBaseUrl];
}

+ (NSString*)getSubmitMeterReadingRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/meter/record", kHttpBaseUrl];
}

+ (NSString*)getThisMonthReadMeterCountUrl{
    return [NSString stringWithFormat:@"%@/api/mix/meter/recorded", kHttpBaseUrl];
}

+ (NSString*)getMyMeterReadingRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/meter/record/personal", kHttpBaseUrl];
}

+ (NSString*)modityMyMeterReadingRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/meter/record", kHttpBaseUrl];
}

+ (NSString*)getThisMonthUnreadMeterEquipmentUrl{
    return [NSString stringWithFormat:@"%@/api/mix/meter/unreading",kHttpBaseUrl];
}
+ (NSString*)getThisMonthReadMeterEquArrayUrl{
    return [NSString stringWithFormat:@"%@/api/mix/meter/read",kHttpBaseUrl];
}
#pragma mark - 通知和临时任务

+ (NSString*)getNoticeUrl{
    return [NSString stringWithFormat:@"%@/api/mix/workNotice", kHttpBaseUrl];
}

+ (NSString*)getTempMissionUrl{
    return [NSString stringWithFormat:@"%@/api/mix/workTask", kHttpBaseUrl];
}

#pragma mark - 评论

+ (NSString*)postReplyUrl{
    return [NSString stringWithFormat:@"%@/api/mix/reply", kHttpBaseUrl];
}

#pragma mark - 上传扫码日志

+ (NSString*)postQrcodeLogUrl{
    return [NSString stringWithFormat:@"%@/api/mix/log/qrcode", kHttpBaseUrl];
}

#pragma mark - 资料库

+ (NSString*)informatiomBankUrl{
    return [NSString stringWithFormat:@"%@/api/mix/repositories", kHttpBaseUrl];
}

#pragma mark - 巡查

+ (NSString*)getInspectionMissionUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/mission", kHttpBaseUrl];
}

+ (NSString*)getInspectionMissionStatusUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/mission/status", kHttpBaseUrl];
}

+ (NSString*)getInspectionRecordUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/record", kHttpBaseUrl];
}

+ (NSString*)getInspectionRecordAndReplyUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/record/latest", kHttpBaseUrl];
}

+ (NSString*)submitInspectionEquipmentRecordUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/equipment/record", kHttpBaseUrl];
}

+ (NSString*)submitInspectionLocationRecordUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/location/record", kHttpBaseUrl];
}

+ (NSString*)getAllInspectionLineUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/line/option", kHttpBaseUrl];
}

+ (NSString*)getInspectionLineDetailUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/line/details", kHttpBaseUrl];
}

+ (NSString*)getOneInspectionLineUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/line", kHttpBaseUrl];
}

+ (NSString*)getEquipmentInspectionRecordUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/equipment/record", kHttpBaseUrl];
}


#pragma mark - 物料

+ (NSString*)materialApplyUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/apply", kHttpBaseUrl];
}

+ (NSString*)materialReceipientUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/stockout", kHttpBaseUrl];
}

+ (NSString*)materialBackUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/return", kHttpBaseUrl];
}

+ (NSString*)materialRearchUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/stock", kHttpBaseUrl];
}

+ (NSString*)createMaterialApplyUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/apply", kHttpBaseUrl];
}

+ (NSString*)addMoreCopierUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/reply", kHttpBaseUrl];
}

+ (NSString*)scanQrcodeReceipientUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/stockout", kHttpBaseUrl];
}

+ (NSString*)scanQrcodeBackUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/return", kHttpBaseUrl];
}

+ (NSString*)cancelMaterialApplyUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/apply", kHttpBaseUrl];
}

+ (NSString*)certurnMaterialStockOutUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/stockout", kHttpBaseUrl];
}

+ (NSString*)certurnMaterialReturnUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/return", kHttpBaseUrl];
}

+ (NSString*)noScanMaterialStockOutUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/stockout/confirm", kHttpBaseUrl];
}

+ (NSString*)noScanCerturnMaterialStockOutUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/stockout/receive/confirm", kHttpBaseUrl];
}

+ (NSString*)noScanRejectMaterialStockOutUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/stockout/receive/reject", kHttpBaseUrl];
}

+ (NSString*)noScanMaterialReturnBackUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/return/confirm", kHttpBaseUrl];
}

+ (NSString*)noScanCerturnMaterialReturnBackUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/return/receive/confirm", kHttpBaseUrl];
}

+ (NSString*)noScanRejectMaterialReturnBackUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/return/receive/reject", kHttpBaseUrl];
}

+ (NSString*)materialMessageWithMaterialRelationIdUrl{
    return [NSString stringWithFormat:@"%@/api/mix/material/message", kHttpBaseUrl];
}
#pragma mark - 记录模块

+ (NSString*)reportRecordUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/report", kHttpBaseUrl];
}

+ (NSString*)repairRecordUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/record", kHttpBaseUrl];
}

+ (NSString*)detectionRecordUrl{
    return [NSString stringWithFormat:@"%@/api/du/record/detection", kHttpBaseUrl];
}

+ (NSString*)tempDetectionRecordUrl{
    return [NSString stringWithFormat:@"%@/api/du/record/temp/detection", kHttpBaseUrl];
}

+ (NSString*)upkeepRecordUrl{
    return [NSString stringWithFormat:@"%@/api/du/record/upkeep", kHttpBaseUrl];
}

+ (NSString*)tempUpkeepRecordUrl{
    return [NSString stringWithFormat:@"%@/api/du/record/temp/upkeep", kHttpBaseUrl];
}

+ (NSString*)keepWatchRecordUrl{
    return [NSString stringWithFormat:@"%@/api/keepwatch/record/module", kHttpBaseUrl];
}

+ (NSString*)inspectionRecordUrl{
    return [NSString stringWithFormat:@"%@/api/inspection/record", kHttpBaseUrl];
}

+ (NSString*)selectAttendanceRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/attendance/record", kHttpBaseUrl];
}

+ (NSString*)selectMeterReadingRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/meter/record", kHttpBaseUrl];
}

#pragma mark - 清点

+ (NSString*)assetCheckRecordUrl{
    return [NSString stringWithFormat:@"%@/api/mix/check", kHttpBaseUrl];
}

#pragma mark - 观察者

+ (NSString *)observerUrl{
    return [NSString stringWithFormat:@"%@/api/mix/observer", kHttpBaseUrl];
}

#pragma mark - 提醒

+ (NSString *)getReminderListUrl{
    return [NSString stringWithFormat:@"%@/api/mix/remind", kHttpBaseUrl];
}

+ (NSString *)getDayReportUrl{
    return [NSString stringWithFormat:@"%@/api/analysis/dayReport", kHttpBaseUrl];
}

#pragma mark - 抢单

+ (NSString *)grabTroubleBillUrl{
    return [NSString stringWithFormat:@"%@/api/trouble/mission/accept", kHttpBaseUrl];
}



#pragma mark - requestHead methods

+ (NSString*)base64Coding:(NSString*)string{
    NSData* Data = [string dataUsingEncoding:NSUTF8StringEncoding];
    Data = [GTMBase64 encodeData:Data];
    return [[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding];
}

+ (NSString*)md5HexEncrypt:(NSString*)string{
    const char* original_str = [string UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, (CC_LONG)strlen(original_str), result);
    NSMutableString* hash = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [hash appendFormat:@"%02X", result[i]];
    return [hash lowercaseString];
}

+ (NSDictionary*)addSubHttpHeadWithDic:(NSDictionary*)dic{
    NSString* appKey = [YWLoginTool requestHeadAppKey];
    NSTimeInterval currentTimeInterval = [[NSDate date] timeIntervalSince1970] * 1000;
    long long currentTime = round(currentTimeInterval);
    long long expireTime = currentTime + 10 * 60 * 1000;
    NSString* timestampString = [NSString stringWithFormat:@"%lld", currentTime];
    NSString* expireString = [NSString stringWithFormat:@"%lld", expireTime];
    NSString* signStr = [[NSString alloc] init];
    signStr = [signStr stringByAppendingFormat:@"%@%@%ld%@", appKey, timestampString, (long)[YWLoginTool loginUser].uniqueId, expireString];
    // YWLog(@"signStr: %@",signStr);
    NSString* codestr = [self base64Coding:signStr];
    codestr = [codestr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
    codestr = [codestr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    codestr = [codestr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    signStr = [self md5HexEncrypt:codestr];
    // YWLog(@"Appkey: %@",appKey);
    // YWLog(@"md5: %@",signStr);

    YWLoginUser* loginUser = [YWLoginTool loginUser];
    NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString* newlocalVersion = [localVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    NSString *deviceType = [YWCommonTool platformStringOfDevice];
    NSDictionary* subHttpHeads = @{ @"timestamp" : timestampString,
        @"expire" : expireString.length ? expireString : @"",
        @"requestId" : [NSString stringWithFormat:@"%ld", (long)[YWLoginTool loginUser].uniqueId],
        @"sign" : signStr.length ? signStr : @"",
        @"versionCode" : newlocalVersion?:@"397",
        @"deviceType" : deviceType,
        @"deviceModel" : deviceType,
        @"deviceVersion" : [[UIDevice currentDevice] systemVersion],
        @"deviceBrand" : @"apple",
        @"clientType" : @"app",
        @"organizationNo" : loginUser.companyAccount ?: @"",
        @"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    NSMutableDictionary* httpHeads = [NSMutableDictionary dictionaryWithDictionary:dic];
    [httpHeads addEntriesFromDictionary:subHttpHeads];
    if ([[httpHeads objectForKey:@"requestId"] integerValue] == 0) {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"requestFailedAndPleaseLoginAgain", nil)];
        return nil;
    }else{
       return httpHeads;
    }
}

#pragma mark - common methods

+ (NSString*)realqrCodeIdWithqrCodeIdString:(NSString*)resuleString{
    NSString *qrCodeId = @"";
    if ([resuleString rangeOfString:@"target="].location != NSNotFound && resuleString.length>19) {
        NSArray *firstArr = [resuleString componentsSeparatedByString:@"?"];
        if (firstArr.count>=2) {
            NSArray *qrcodeArr = [firstArr[1] componentsSeparatedByString:@"target="];
            if (qrcodeArr.count>=2) {
                qrCodeId = qrcodeArr[1];
            }
        }
    }else {
        qrCodeId = resuleString;
    }
    return qrCodeId;
}

+ (void)showTextHUDWithContent:(NSString*)content{
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD* hud = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
        [[UIApplication sharedApplication].keyWindow addSubview:hud];
        
        // Configure for text only and offset down
        hud.mode = MBProgressHUDModeText;
        hud.labelFont = kHUDLabelFont;
        hud.labelText = content;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        hud.animationType = MBProgressHUDAnimationZoomOut;
        [hud show:YES];
        [hud hide:YES afterDelay:1];
    });
}

+ (NSString*)maintenanceTypeOfMaintenanceTypeId:(NSInteger)maintenanceTypeId{
    NSString* type = NSLocalizedString(@"maintenance", nil);//@"维保";
    switch (maintenanceTypeId) {
        case YWMaintenanceTypePatrol:
            type = NSLocalizedString(@"inspection", nil);//@"巡查";
            break;
        case YWMaintenanceTypeDetection:
            type = NSLocalizedString(@"detection", nil);//@"检测";
            break;
        case YWMaintenanceTypeUpkeep:
            type = NSLocalizedString(@"upkeep", nil);//@"保养";
            break;
        case YWMaintenanceTypeTrouble:
            type = NSLocalizedString(@"trouble", nil);//@"维修";
            break;
        case YWMaintenanceTypeKeepWatch:
            type = NSLocalizedString(@"keepWatch", nil);//@"巡更";
            break;
        default:
            break;
    }
    return type;
}

+ (NSString*)getFileSize:(long)size{
    long kb = 1024;
    long mb = kb * 1024;
    long gb = mb * 1024;
    
    if (size >= gb) {
        return [NSString stringWithFormat:@"%.1f GB",(float) (size / gb) ];
    } else if (size >= mb) {
        float f = (float) size / mb;
        if (f > 100 ) {
            return [NSString stringWithFormat:@"%.0f MB", f];
        }else{
            return [NSString stringWithFormat:@"%.1f MB", f];
        }
        
    } else if (size >= kb) {
        float f = (float) size / kb;
        if (f > 100 ) {
            return [NSString stringWithFormat:@"%.0f KB", f];
        }else{
            return [NSString stringWithFormat:@"%.1f KB", f];
        }
        
    } else{
        return [NSString stringWithFormat:@"%ld B", size];
    }
}

+ (NSString*)minuteToHourOrDayString:(NSInteger)minutes{
    NSString *hourOrDayString = NSLocalizedString(@"none", nil);//@"无";
    if (minutes<60 && minutes>0) {
        hourOrDayString = [NSString stringWithFormat:@"%ld%@",(long)minutes,NSLocalizedString(@"minute", nil)];
    }else if (minutes>=60 && minutes<60*24){
        hourOrDayString = [NSString stringWithFormat:@"%ld%@",(long)minutes/60,NSLocalizedString(@"hour", nil)];
    }else if (minutes>=60*24){
        hourOrDayString = [NSString stringWithFormat:@"%ld天",(long)minutes/60/24];
    }
    return hourOrDayString;
}

+ (NSString*)randStringId{
    NSInteger charBits = 16;
    char data[charBits];
    
    for (int x = 0; x < charBits; x++) {
        if (arc4random() % 2 == 0) {
            data[x] = (char)('A' + (arc4random_uniform(26)));
        }else {
            data[x] = (char)('0' + arc4random() % 10);
        }
    }
    return [[NSString alloc] initWithBytes:data length:charBits encoding:NSUTF8StringEncoding];
}

+ (unsigned long)randNumber{
    NSInteger charBits = 9;//十位的话会超出unsignedLongValue的范围
    char data[charBits];
    
    data[0] = (char)('1' + (arc4random() % 9));
    for (int x = 1; x < charBits; x++) {
        data[x] = (char)('0' + arc4random() % 10);
    }
    long long randomNum = [[[NSString alloc] initWithBytes:data length:charBits encoding:NSUTF8StringEncoding] longLongValue];
    NSNumber *numbe = [NSNumber numberWithLongLong:llabs(randomNum)];
    return [numbe unsignedLongValue];
}

+ (long long)negativeTimestampId{
    NSTimeInterval nowTimeInterval = [[NSDate date] timeIntervalSince1970];
    double nowTimeIntervalId = (double)nowTimeInterval *1000 *1000;
    long long timeLong = (long long)nowTimeIntervalId ;
    return timeLong;
}

+ (NSString*)shortDatePath{
    NSDate* date = [NSDate date];
    NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//NSGregorianCalendar
    NSDateComponents* comps = nil;
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;//
    comps = [calendar components:unitFlags fromDate:date];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    return [NSString stringWithFormat:@"%ld/%.2ld/%.2ld", (long)year, (long)month, (long)day];
}

+ (NSString*)showTime:(NSTimeInterval)time andFormatter:(NSString*)formatterStr{
    if (time) {
        struct tm *timeinfo;
        char buffer[80];
        const char *format = [formatterStr UTF8String];
        time_t rawtime = (time_t)time;
        timeinfo = localtime(&rawtime);
        strftime(buffer, 80, format, timeinfo);//"%y-%m-%d %H:%M"
        return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
    }else {
        return @" ";
    }
}

+ (NSString*)showTimeWithNormalFormatter:(NSTimeInterval)time{
    return [self showTime:time andFormatter:KTimeFormatShortTimeDateYear];
}

+ (NSString*)showDateAndTime:(NSTimeInterval)time{
    return [self showTime:time andFormatter:KTimeFormatDateAndTime];
}

+ (NSString*)showDate:(NSTimeInterval)time{
    return [self showTime:time andFormatter:KTimeFormatDate];
}

+ (NSString*)showChineseFormatterDate:(NSTimeInterval)time{
    return [self showTime:time andFormatter:KTimeFormatChineseDate];
}

+ (NSString*)showFullTime:(NSTimeInterval)time{
    return [self showTime:time andFormatter:KTimeFormatFullTime];
}

+ (NSString*)showDateAndsecond:(NSTimeInterval)time{
    return [self showTime:time andFormatter:KTimeFormatFullDateAndTime];
}

+ (long long)numberWithTime:(NSTimeInterval)time{
    long long dTime = [[NSNumber numberWithDouble:time] longLongValue];
    return dTime;
}

+ (NSString *)handlerNamesWithHandlers:(NSArray*)handlers{
     NSMutableString* toUserNames = [NSMutableString string];
    [handlers enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx != 0) {
            [toUserNames appendString:@","];
        }
        [toUserNames appendString:[NSString stringWithFormat:@"%@",handler.userName.length ? handler.userName : @" "]];
    }];
    
    return toUserNames.length?[NSString stringWithFormat:@"%@",toUserNames]:@"";
}

+ (NSString*)handlerIdsWithHandlerArray:(NSArray*)handlers{
    NSMutableString* handlerIds = [NSMutableString string];
    [handlers enumerateObjectsUsingBlock:^(YWHandler* _Nonnull handler, NSUInteger idx, BOOL* _Nonnull stop) {
        if (idx != 0) {
            [handlerIds appendString:@","];
        }
        [handlerIds appendString:[NSString stringWithFormat:@"%ld",(long)handler.uniqueId]];
    }];
    return handlerIds;
}

+ (BOOL)containSelfInHandlers:(NSArray*)handlers{
    __block BOOL contain = NO;
    __block YWLoginUser* loginUser = [YWLoginTool loginUser];
    NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", loginUser.uniqueId];
    NSArray *array = [[handlers filteredArrayUsingPredicate:searchPredicate] mutableCopy];
    if (array.count > 0) {
        contain = YES;
    }
    return contain;
}

+ (NSString*)uniqueTagWithMission:(id)mission{
    NSArray *looperTimeArray = [YWCommonTool looperTimeArrayWithMission:mission];
    long long lineRecordStarTime = 0;
    if (looperTimeArray.count>0) {
        lineRecordStarTime = [[looperTimeArray firstObject] longLongValue]*1000;
    }else{
        lineRecordStarTime = [self timeStampToInteger:[[NSDate date] timeIntervalSince1970]]/1000;
    }
    
    NSInteger missionId = 0;
    NSString *uniqueTag = nil;
    if ([mission isKindOfClass:[YWInspectionMissionModel class]]) {
        YWInspectionMissionModel *inspectionMission = (YWInspectionMissionModel*)mission;
        missionId = inspectionMission.inspectionMissionId;
        uniqueTag = [NSString stringWithFormat:@"%ld_%lld",(long)missionId,lineRecordStarTime];
    }else if ([mission isKindOfClass:[YWKeepWatchMission class]]){
        YWKeepWatchMission *keepWatchMission = (YWKeepWatchMission*)mission;
        missionId = keepWatchMission.keepWatchMissionId;
        uniqueTag = [NSString stringWithFormat:@"%ld_%lld",(long)missionId,lineRecordStarTime];
    }
    return uniqueTag;
}

+ (NSArray*)looperTimeArrayWithMission:(id)mission{
    //间隔
    NSInteger intervalMinute = 0;
    //是否是在工作时间内
    BOOL isWorkTime = YES;
    
    if ([mission isKindOfClass:[YWInspectionMissionModel class]]) {
        //巡查
        YWInspectionMissionModel *inspectionMission = (YWInspectionMissionModel*)mission;
        YWInspectionJudgeType judgeType = [[YWInspectionMissionManager shareManager] judgeTypeWithMission:inspectionMission];
        isWorkTime = judgeType == YWInspectionJudgeTypeMatch ? YES :NO;
        if (isWorkTime) {
            intervalMinute = inspectionMission.inspectionLine.intervalMinute;
            //这里判断间隔是因为巡查有星期的情况
            if (intervalMinute>24*60) {
                //从任务的开始日期的0点开始算
                return [self timeSpaceOfMorethanTwentyfourHourWithIntervalMinute:intervalMinute startDate:inspectionMission.startDate];
            }else{
                //从每天的0点开始算
                return [self timeSpaceOfLessthanTwentyfourHourWithIntervalMinute:intervalMinute startDate:inspectionMission.startDate endDate:inspectionMission.endDate startTime:inspectionMission.startTime endTime:inspectionMission.endTime];
            }
        }else{
            return nil;
        }
        
    }else if ([mission isKindOfClass:[YWKeepWatchMission class]]){
        //巡更
        YWKeepWatchMission *keepWatchMission = (YWKeepWatchMission*)mission;
        YWKeepWatchJudgeType judgeType = [[YWKeepWatchMissionManager shareManager] judgeTypeWithMission:keepWatchMission];
        isWorkTime = judgeType == YWKeepWatchJudgeTypeMatch ? YES :NO;
        if (isWorkTime) {
            return [self timeSpaceOfLessthanTwentyfourHourWithIntervalMinute:keepWatchMission.patrolLine.intervalMinute startDate:keepWatchMission.startDate endDate:keepWatchMission.endDate startTime:keepWatchMission.startTime endTime:keepWatchMission.endTime];
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}

/*-------------间隔超过一天（超过24小时）-------------*/
+ (NSArray*)timeSpaceOfMorethanTwentyfourHourWithIntervalMinute:(NSInteger)intervalMinute startDate:(NSTimeInterval)startDate{
    
    NSTimeInterval startInterval = 0;
    NSTimeInterval endInterval = 0;
    long long intervalMinuteLonglong = intervalMinute;
    long long missionIntervalTime = intervalMinuteLonglong*60*1000*1000;
    
    long long startLonglong = [YWCommonTool timeStampToInteger:startDate/1000];
    long long currentTime = [YWCommonTool timeStampToInteger:[[NSDate date] timeIntervalSince1970]];
    long long totalInterval = currentTime - startLonglong;
    long long interval = missionIntervalTime;//*60*1000*1000;
    long long looperCout = totalInterval/interval;
    
    long long startDouble = startLonglong + looperCout*missionIntervalTime;//*60*1000*1000;
    startInterval = startDouble/(1000*1000);
    
    endInterval = (startDouble + missionIntervalTime)/(1000*1000);
    //这里结束时间－1，是因为现在间隔三天显示的是3/21-3/24，应该显示3/21-3/23
    return [NSArray arrayWithObjects:@(startInterval),@(endInterval-1), nil];
}

/*-------------间隔事一天之内（24小时内）目前的巡更的间隔时间不会超过一天-------------*/
+ (NSArray*)timeSpaceOfLessthanTwentyfourHourWithIntervalMinute:(NSInteger)intervalMinute startDate:(NSTimeInterval)startDate endDate:(NSTimeInterval)endDate startTime:(NSString *)startTime endTime:(NSString*)endTime{
    //间隔
    long long intervalMinuteLonglong = intervalMinute;
    long long missionIntervalTime = intervalMinuteLonglong*60*1000*1000;//intervalMinute*60*1000*1000(这样写在32位机型上溢出了);
    //一天的时间戳
    long long oneDay = 86400000000;//即(24*60*64*1000*1000)
    //当前时间
    long long currentTimeInterval = [YWCommonTool timeStampToInteger:[[NSDate date] timeIntervalSince1970]];
    
    //算第一个区段
    NSString *startDateStr = [YWCommonTool showDate:startDate/1000];
    NSString *startStr= [NSString stringWithFormat:@"%@ %@:00",startDateStr,startTime];
    //开始和结束时间 都是以当天日期拼任务的是startTime
    NSTimeInterval startDateInterVal = [YWCommonTool timeIntervalWithDatestring:startStr dateFormatter:KTimeFormatFullDateAndTime];
    long long startLonglong = [YWCommonTool timeStampToInteger:startDateInterVal];
    NSString *endStr = [NSString stringWithFormat:@"%@ %@:00",startDateStr,endTime];
    NSTimeInterval endDateInterVal = [YWCommonTool timeIntervalWithDatestring:endStr dateFormatter:KTimeFormatFullDateAndTime];
    long long endLonglong = [YWCommonTool timeStampToInteger:endDateInterVal];
    //比较是否(跨天)隔夜
    if (endLonglong <= startLonglong) {
        //隔夜
        endLonglong += oneDay;
    }
    
    //任务结束日期
    long long missionEndDateInterval = [YWCommonTool timeStampToInteger:endDate/1000];
    
    while (startLonglong < missionEndDateInterval) {
        if (currentTimeInterval>startLonglong && currentTimeInterval<endLonglong) {
            for (long long i = startLonglong ; i<endLonglong; i+=missionIntervalTime) {
                long long endTimeInterv = i+missionIntervalTime;
                if (currentTimeInterval>i && currentTimeInterval<=endTimeInterv) {
                    if (endTimeInterv>endLonglong) {
                        return [NSArray arrayWithObjects:@(i/(1000*1000)),@(endLonglong/(1000*1000)), nil];
                    }else{
                        return [NSArray arrayWithObjects:@(i/(1000*1000)),@(endTimeInterv/(1000*1000)), nil];
                    }
                }
            }
        }
        startLonglong += oneDay;
        endLonglong += oneDay;
    }
    
    return nil;
}

+ (long long)timeStampToInteger:(NSTimeInterval)timeStamp{
    if (timeStamp>0) {
        //保证返回的是16位
        long long timeLong = (long long)(timeStamp *1000 *1000);
        NSString *timeStampString = [@(timeLong) stringValue];
        NSUInteger length = timeStampString.length;
        if (length == 16) {
            return timeLong;
        }else if (length > 16){
            timeStampString = [timeStampString substringToIndex:16];
            timeLong = [timeStampString longLongValue];
            return timeLong;
        }else{
            while (timeStampString.length<16) {
                timeStampString = [timeStampString stringByAppendingString:@"0"];
            }
            timeLong = [timeStampString longLongValue];
            return timeLong;
        }
    }else{
        return 0;
    }
}

+ (NSTimeInterval)timeIntervalWithDatestring:(NSString*)timeStr dateFormatter:(NSString*)formatterStr{
//    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:formatterStr];
//    NSDate* date = [formatter dateFromString:timeStr];
    if (!timeStr) {
        return 0;
    }
    struct tm tm;
    time_t t;
    const char *format = [formatterStr UTF8String];
    strptime([timeStr cStringUsingEncoding:NSUTF8StringEncoding], format, &tm);
    tm.tm_isdst = -1;
    t = mktime(&tm);
    //NSDate* date = [NSDate dateWithTimeIntervalSince1970:t];
    //NSTimeInterval timeInterval = [date timeIntervalSince1970];
    NSTimeInterval timeInterval = (NSTimeInterval)t;
    return timeInterval;
}

+ (NSDate*)getNowDateFromatAnyDate:(NSDate*)anyDate{
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; //或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    //转为现在时间
    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
    return destinationDateNow;
}

+ (CGFloat)getHeightOfLabel:(NSString*)content andWidth:(CGFloat)width andFont:(UIFont*)font andMinHeight:(CGFloat)minHeight{
    //设置一个行高上限
    CGSize size = CGSizeMake(width, MAXFLOAT);
    NSDictionary* attributes = @{ NSFontAttributeName : font };
    CGSize labelsize = [content boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    if (minHeight > 0) {
        return (labelsize.height >= minHeight) ? labelsize.height : minHeight;
    }
    return labelsize.height;// + 5;
}

+ (CGRect)getSizeOfLabel:(NSString*)content andWidth:(CGFloat)width andFont:(UIFont*)font{
    //设置一个行高上限
    CGSize size = CGSizeMake(width, MAXFLOAT);
    NSDictionary* attributes = @{ NSFontAttributeName : font };
    CGRect labelsize = [content boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil];
    return labelsize;
}

+ (NSAttributedString*)attributedTextWithFirstText:(NSString *)firstText firstColor:(UIColor *)firstColor secondText:(NSString *)secondText secondColor:(UIColor *)secondColor font:(UIFont *)font{
    if (secondText == nil){
        return nil;
    }
    NSString *fullText = [NSString stringWithFormat:@"%@%@",firstText,secondText];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:fullText];
    text.font = font ?: kContentFont;
    [text setColor:firstColor?:[UIColor customTextGrayColor] range:NSMakeRange(0, firstText.length)];
    [text setColor:secondColor?:[UIColor blackColor] range:NSMakeRange(firstText.length, secondText.length)];
    text.lineSpacing = 5;
    //text.lineBreakMode = NSLineBreakByWordWrapping;
    return text;
}

+ (NSAttributedString*)attributedTextWithFirstText:(NSString *)firstText firstColor:(UIColor *)firstColor firstFont:(UIFont *)firstFont secondText:(NSString *)secondText secondColor:(UIColor *)secondColor secFont:(UIFont *)secFont thirdText:(NSString*)thirdText thirdColor:(UIColor*)thirdColor thirdFont:(UIFont*)thirdFont fourthText:(NSString*)fourthText fourthColor:(UIColor*)fourthColor fourFont:(UIFont*)fourFont{
    if (thirdText == nil){
        return nil;
    }
    NSString *fullText = [NSString stringWithFormat:@"%@%@%@%@",firstText,secondText,thirdText,fourthText];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:fullText];
    
    [text setFont:firstFont range:NSMakeRange(0, firstText.length)];
    [text setFont:secFont range:NSMakeRange(firstText.length, secondText.length)];
    [text setFont:thirdFont range:NSMakeRange((firstText.length+secondText.length), thirdText.length)];
    [text setFont:fourFont range:NSMakeRange((firstText.length+secondText.length+thirdText.length), fourthText.length)];
    
    [text setColor:firstColor range:NSMakeRange(0, firstText.length)];
    [text setColor:secondColor range:NSMakeRange(firstText.length, secondText.length)];
    [text setColor:thirdColor range:NSMakeRange((firstText.length+secondText.length), thirdText.length)];
    [text setColor:fourthColor range:NSMakeRange((firstText.length+secondText.length+thirdText.length), fourthText.length)];
    text.lineSpacing = 5;
    return text;
}

+ (NSAttributedString*)attributedTextWithFirstText:(NSString *)firstText firstColor:(UIColor *)firstColor firstFont:(UIFont *)firstFont secondText:(NSString *)secondText secondColor:(UIColor *)secondColor secFont:(UIFont *)secFont thirdText:(NSString*)thirdText thirdColor:(UIColor*)thirdColor thirdFont:(UIFont*)thirdFont fourthText:(NSString*)fourthText fourthColor:(UIColor*)fourthColor fourFont:(UIFont*)fourFont fifthText:(NSString *)fifthText fifthColor:(UIColor*)fifthColor fifthFont:(UIFont*)fifthFont{
    if (fourthText == nil){
        return nil;
    }
    NSString *fullText = [NSString stringWithFormat:@"%@%@%@%@%@",firstText,secondText,thirdText,fourthText,fifthText];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:fullText];
    
    [text setFont:firstFont range:NSMakeRange(0, firstText.length)];
    [text setFont:secFont range:NSMakeRange(firstText.length, secondText.length)];
    [text setFont:thirdFont range:NSMakeRange((firstText.length+secondText.length), thirdText.length)];
    [text setFont:fourFont range:NSMakeRange((firstText.length+secondText.length+thirdText.length), fourthText.length)];
    [text setFont:fifthFont range:NSMakeRange((firstText.length+secondText.length+thirdText.length+fourthText.length), fifthText.length)];
    
    [text setColor:firstColor range:NSMakeRange(0, firstText.length)];
    [text setColor:secondColor range:NSMakeRange(firstText.length, secondText.length)];
    [text setColor:thirdColor range:NSMakeRange((firstText.length+secondText.length), thirdText.length)];
    [text setColor:fourthColor range:NSMakeRange((firstText.length+secondText.length+thirdText.length), fourthText.length)];
    [text setColor:fifthColor range:NSMakeRange((firstText.length+secondText.length+thirdText.length+fourthText.length), fifthText.length)];
    text.lineSpacing = 5;
    return text;
}

+ (UIView*)lineOfView:(CGRect)rect{
    UIView* lineView = [[UIView alloc] initWithFrame:rect];
    [lineView setBackgroundColor:[UIColor customLineColor]];
    return lineView;
}

+ (BOOL)isContainsEmoji:(NSString*)string{
    __block BOOL returnValue = NO;
    if ([string containsString:@"$"] || [string containsString:@"&"]) {
        return YES;
    }
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {
        const unichar hs = [substring characterAtIndex:0];
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    returnValue = YES;
                }
            }
        }
        else if (substring.length > 1) {
            const unichar ls = [substring characterAtIndex:1];
            if (ls == 0x20e3) {
                returnValue = YES;
            }
        }
        else {
            if (0x2100 <= hs && hs <= 0x27ff) {
                returnValue = YES;
            }
            else if (0x2B05 <= hs && hs <= 0x2b07) {
                returnValue = YES;
            }
            else if (0x2934 <= hs && hs <= 0x2935) {
                returnValue = YES;
            }
            else if (0x3297 <= hs && hs <= 0x3299) {
                returnValue = YES;
            }
            else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                returnValue = YES;
            }
        }
    }];

    return returnValue;
}

+ (BOOL)isEwinQrcode:(NSString*)qrcodeId{
    __block BOOL returnValue = NO;
    if (qrcodeId.length >= 19) {
        int sequence = [[qrcodeId substringWithRange:(NSRange){ 0, 12 }] intValue];
        int random = [[qrcodeId substringWithRange:(NSRange){ 13, 4 }] intValue];
        int check = [[qrcodeId substringWithRange:(NSRange){ 18, 1 }] intValue];
        if ((sequence != 0 || random != 0) && ((sequence % 9 + random % 8) % 10 == check)) {
            returnValue = YES;
        }
    }
    return returnValue;
}

+ (NSString*)platformStringOfDevice{
    return [[UIDevice currentDevice] machineModelName];
}

+ (NSString*)recordDateByFormatTimeString:(NSTimeInterval)time showYear:(BOOL)showYear{
    if (!time) {
        return @" ";
    }
//    NSDate* inputDate = [NSDate dateWithTimeIntervalSince1970:time];
//
//    NSDateFormatter* outputFormatter = [[NSDateFormatter alloc] init];
//    [outputFormatter setLocale:[NSLocale currentLocale]];
//    [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    //get date str
//    NSString* str = [outputFormatter stringFromDate:inputDate];
//    //str to nsdate
//    NSDate* strDate = [outputFormatter dateFromString:str];
    
    NSDate* strDate = [NSDate dateWithTimeIntervalSince1970:time];
    
    //修正8小时的差时
    NSTimeZone* zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:strDate];
    NSDate* endDate = [strDate dateByAddingTimeInterval:interval];
    NSString* lastTime = [self compareDate:endDate showYear:showYear];
    return lastTime; //str;
}

+ (NSString*)compareDate:(NSDate*)date showYear:(BOOL)showYear{
    if (!date) {
        return nil;
    }
    NSTimeInterval secondsPerDay = 24 * 60 * 60;

    //修正8小时之差
    NSDate* date1 = [NSDate date];
    NSTimeZone* zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date1];
    NSDate* localeDate = [date1 dateByAddingTimeInterval:interval];

    NSDate* today = localeDate;
    NSDate* yesterday; // *beforeOfYesterday;
    //今年
    NSString* toYears;

    toYears = [[today description] substringToIndex:4];

    yesterday = [today dateByAddingTimeInterval:-secondsPerDay];
    //beforeOfYesterday = [yesterday dateByAddingTimeInterval:-secondsPerDay];

    // 10 first characters of description is the calendar date:
    NSString* todayString = [[today description] substringToIndex:10];
    NSString* yesterdayString = [[yesterday description] substringToIndex:10];
    //NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10];

    NSString* dateString = [[date description] substringToIndex:10];
    NSString* dateYears = [[date description] substringToIndex:4];

    NSString* dateContent;
    if ([dateYears isEqualToString:toYears]) { //同一年
        //今 昨 前天的时间(今天：10：12)
        NSString* time = [[date description] substringWithRange:(NSRange){ 11, 5 }];
        //其他时间(01-6)
        NSString* time2 = [[date description] substringWithRange:(NSRange){ 5, 5 }];
        if ([dateString isEqualToString:todayString]) {
            dateContent = [NSString stringWithFormat:@"今天 %@", time];
            return dateContent;
        }
        else if ([dateString isEqualToString:yesterdayString]) {
            dateContent = [NSString stringWithFormat:@"昨天 %@", time];
            return dateContent;
        }
        else {
            NSString* month = [time2 stringByReplacingOccurrencesOfString:@"-" withString:@"月"];
            return [NSString stringWithFormat:@"%@日", month];
        }
    }
    else {
        NSString* year = [dateString substringWithRange:(NSRange){ 0, 4 }];
        NSString* month = [dateString substringWithRange:(NSRange){ 5, 2 }];
        NSString* day = [dateString substringWithRange:(NSRange){ 8, 2 }];
        if (showYear) {
            return [NSString stringWithFormat:@"%@年%@月%@日", year, month, day];
        }
        else {
            return [NSString stringWithFormat:@"%@月%@日", month, day];
        }
    }
}

+ (NSMutableAttributedString*)labelAttributedStringWithFristStr:(NSString*)firstStr secondStr:(NSString*)secondStr firstColor:(UIColor*)firstColor{
    NSMutableAttributedString* firstAttriburedStr = [[NSMutableAttributedString alloc] initWithString:firstStr];
    NSMutableAttributedString* secondAttriburedStr = [[NSMutableAttributedString alloc] initWithString:secondStr];

    [firstAttriburedStr addAttribute:NSForegroundColorAttributeName value:firstColor range:NSMakeRange(0, firstAttriburedStr.length)];
    [secondAttriburedStr addAttribute:NSForegroundColorAttributeName value:[UIColor customTextGrayColor] range:NSMakeRange(0, secondAttriburedStr.length)];
    [firstAttriburedStr appendAttributedString:secondAttriburedStr];
    return firstAttriburedStr;
}

+ (NSMutableAttributedString*)labelAttributedStringWithFristStr:(NSString*)firstStr secondStr:(NSString*)secondStr secondColor:(UIColor*)secondColor{
    NSMutableAttributedString* firstAttriburedStr = [[NSMutableAttributedString alloc] initWithString:firstStr];
    NSMutableAttributedString* secondAttriburedStr = [[NSMutableAttributedString alloc] initWithString:secondStr];

    [firstAttriburedStr addAttribute:NSForegroundColorAttributeName value:[UIColor customTextGrayColor] range:NSMakeRange(0, firstAttriburedStr.length)];
    [secondAttriburedStr addAttribute:NSForegroundColorAttributeName value:secondColor range:NSMakeRange(0, secondAttriburedStr.length)];
    [firstAttriburedStr appendAttributedString:secondAttriburedStr];
    return firstAttriburedStr;
}

+ (NSInteger)getDaysFrom:(NSDate*)startDate To:(NSDate*)endDate{
    NSCalendar* gregorian = [[NSCalendar alloc]
        initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [gregorian setFirstWeekday:2];

    //去掉时分秒信息
    NSDate* fromDate;
    NSDate* toDate;
    [gregorian rangeOfUnit:NSCalendarUnitDay startDate:&fromDate interval:NULL forDate:startDate];
    [gregorian rangeOfUnit:NSCalendarUnitDay startDate:&toDate interval:NULL forDate:endDate];
    NSDateComponents* dayComponents = [gregorian components:NSCalendarUnitDay fromDate:fromDate toDate:toDate options:0];

    return dayComponents.day;
}

//获取设备尺寸
+ (NSString*)inchOfDevice{
    // 获取设备名称
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString* platform = [NSString stringWithFormat:@"%s", systemInfo.machine];

    NSString* inch = @"";
    if ([platform isEqualToString:@"iPhone1,1"]
        || [platform isEqualToString:@"iPhone1,2"]
        || [platform isEqualToString:@"iPhone2,1"]
        || [platform isEqualToString:@"iPhone3,1"]
        || [platform isEqualToString:@"iPhone3,2"]
        || [platform isEqualToString:@"iPhone3,3"]
        || [platform isEqualToString:@"iPhone4,1"]
        || [platform isEqualToString:@"iPod4,1"]
        || [platform isEqualToString:@"iPod1,1"]
        || [platform isEqualToString:@"iPod2,1"]
        || [platform isEqualToString:@"iPod3,1"]) {
        inch = @"3.5寸";
    }
    else if ([platform isEqualToString:@"iPhone5,1"]
        || [platform isEqualToString:@"iPhone5,2"]
        || [platform isEqualToString:@"iPhone5,3"]
        || [platform isEqualToString:@"iPhone5,4"]
        || [platform isEqualToString:@"iPhone6,1"]
        || [platform isEqualToString:@"iPhone6,2"]
        || [platform isEqualToString:@"iPod5,1"]) {
        inch = @"4寸";
    }
    else if ([platform isEqualToString:@"iPhone7,1"]) {
        inch = @"5.5寸";
    }
    else if ([platform isEqualToString:@"iPhone7,2"]) {
        inch = @"4.7寸";
    }
    else if ([platform isEqualToString:@"iPad1,1"]
        || [platform isEqualToString:@"iPad2,1"]
        || [platform isEqualToString:@"iPad2,2"]
        || [platform isEqualToString:@"iPad2,3"]
        || [platform isEqualToString:@"iPad2,4"]
        || [platform isEqualToString:@"iPad3,1"]
        || [platform isEqualToString:@"iPad3,2"]
        || [platform isEqualToString:@"iPad3,3"]
        || [platform isEqualToString:@"iPad3,4"]
        || [platform isEqualToString:@"iPad3,5"]
        || [platform isEqualToString:@"iPad3,6"]
        || [platform isEqualToString:@"iPad4,1"]
        || [platform isEqualToString:@"iPad4,2"]
        || [platform isEqualToString:@"iPad4,3"]) {
        inch = @"9.7寸";
    }
    else if ([platform isEqualToString:@"iPad2,5"]
        || [platform isEqualToString:@"iPad2,6"]
        || [platform isEqualToString:@"iPad2,7"]
        || [platform isEqualToString:@"iPad4,4"]
        || [platform isEqualToString:@"iPad4,5"]
        || [platform isEqualToString:@"iPad4,6"]) {
        // mini
        inch = @"7.9寸";
    }
    else {
        inch = @"未知尺寸";
    }

    return inch;
}

+ (NSString*)netType{
    UIApplication* app = [UIApplication sharedApplication];
    NSArray* subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
    NSNumber* dataNetworkItemView = nil;

    for (id subview in subviews) {
        if ([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }

    NSString* netType = @"";
    NSNumber* num = [dataNetworkItemView valueForKey:@"dataNetworkType"];
    if (num == nil) {

        netType = @"";
    }
    else {
        int n = [num intValue];
        //YWLog(@"dataNetworkType:%d",n);
        if (n == 0) {
            netType = @"";
        }
        else if (n == 1) {
            netType = @"2g";
        }
        else if (n == 2) {
            netType = @"3g";
        }
        else {
            netType = @"wifi";
        }
    }

    return netType;
}

+ (NSString*)weekDayString:(NSInteger)weekDay{
    NSString* weekDayString = nil;
    switch (weekDay) {
    case 0:
        weekDayString = @"周日";
        break;
    case 1:
        weekDayString = NSLocalizedString(@"monday", nil);//@"周一";
        break;
    case 2:
        weekDayString = @"周二";
        break;
    case 3:
        weekDayString = @"周三";
        break;
    case 4:
        weekDayString = @"周四";
        break;
    case 5:
        weekDayString = @"周五";
        break;
    case 6:
        weekDayString = @"周六";
        break;
    }
    return weekDayString;
}

+ (NSInteger)weekOfString:(NSString*)weekString{
    NSInteger week = 0;
    if ([weekString isEqualToString:@"周日"]) {
        week = 0;
    }else if ([weekString isEqualToString:NSLocalizedString(@"monday", nil)]){
        week = 1;
    }else if ([weekString isEqualToString:@"周二"]){
        week = 2;
    }else if ([weekString isEqualToString:@"周三"]){
        week = 3;
    }else if ([weekString isEqualToString:@"周四"]){
        week = 4;
    }else if ([weekString isEqualToString:@"周五"]){
        week = 5;
    }else if ([weekString isEqualToString:@"周六"]){
        week = 6;
    }
    return week;
}
@end
